#!/bin/bash


Install_MariaDB()
{

    echo -e "\033[34m[+] Installing ${MariaDB_Ver}...\033[0m"

    cd ${cur_dir}/src
    tar zxf ${MariaDB_Ver}.tar.gz
    cd ${MariaDB_Ver}
    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mariadb -DWITH_ARIA_STORAGE_ENGINE=1 -DWITH_XTRADB_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DWITH_READLINE=1 -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 ${MariaDBMAOpt}
    make -j `grep 'processor' /proc/cpuinfo | wc -l`
    if [ $? -ne 0 ]; then
        make
    fi
    make install

    groupadd mariadb
    useradd -s /sbin/nologin -M -g mariadb mariadb
    cat > /etc/my.cnf<<EOF
[client]
#password	= your_password
port		= 3306
socket		= /tmp/mysql.sock

[mysqld]
port		= 3306
socket		= /tmp/mysql.sock
user    = mariadb
basedir = /usr/local/mariadb
datadir = ${DB_Data_Dir}
log_error = ${DB_Data_Dir}/mariadb.err
pid-file = ${DB_Data_Dir}/mariadb.pid
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
table_open_cache = 64
explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
server-id	= 1
bind-address = 0.0.0.0
log-bin=mysql-bin
binlog_format=mixed
expire_logs_days = 10
default_storage_engine = InnoDB
innodb_data_home_dir = ${DB_Data_Dir}
innodb_data_file_path = ibdata1:10M:autoextend
innodb_buffer_pool_size	= 256M
innodb_log_file_size	= 50M
innodb_log_buffer_size	= 8M
innodb_flush_log_at_trx_commit = 2
innodb_lock_wait_timeout= 50
innodb_file_per_table	= 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
EOF

    mkdir -p ${DB_Data_Dir}
    chown -R mariadb:mariadb ${DB_Data_Dir}
    /usr/local/mariadb/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mariadb --datadir=${DB_Data_Dir} --user=mariadb
    chgrp -R mariadb /usr/local/mariadb/.
    \cp support-files/mysql.server /etc/init.d/mariadb
    chmod 755 /etc/init.d/mariadb

    cat > /etc/ld.so.conf.d/mariadb.conf<<EOF
/usr/local/mariadb/lib
/usr/local/lib
EOF
    ldconfig

    if [ -d "/proc/vz" ];then
        ulimit -s unlimited
    fi

    if [ "$PM" = "yum" ]; then
        chkconfig --add mariadb
        chkconfig mariadb on
     elif [ "$PM" = "apt" ]; then
        update-rc.d -f mariadb defaults
    fi
    /etc/init.d/mariadb start

    ln -sf /usr/local/mariadb/bin/mysql /usr/bin/mysql
    ln -sf /usr/local/mariadb/bin/mysqldump /usr/bin/mysqldump
    ln -sf /usr/local/mariadb/bin/myisamchk /usr/bin/myisamchk
    ln -sf /usr/local/mariadb/bin/mysqld_safe /usr/bin/mysqld_safe
    ln -sf /usr/local/mariadb/bin/mysqlcheck /usr/bin/mysqlcheck

    /usr/local/mariadb/bin/mysqladmin -u root password ${DB_Root_Password}
    cat > /tmp/mariadb_sec_script<<EOF
use mysql;
update user set password=password('${DB_Root_Password}') where user='root';
delete from user where not (user='root') ;
delete from user where user='root' and password=''; 
drop database test;
DROP USER ''@'%';
flush privileges;
EOF
    /usr/local/mariadb/bin/mysql -u root -p${DB_Root_Password} -h localhost < /tmp/mariadb_sec_script
    rm -f /tmp/mariadb_sec_script
    /etc/init.d/mariadb restart
    rm -fr ${cur_dir}/src/${MariaDB_Ver}

}
